﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;

namespace BuXiaChu.OrderDishes.Web.Admin.Sys
{
    public partial class SetRoles : System.Web.UI.Page
    {
        protected SysUserInfoModel sysUserInfo = null;
        protected List<RolesModel> roleList = null;
        protected List<RolesModel> userRoleList = null;
        protected int saveStatus = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == true)
            {
                Save();
            }
            InitData();
        }

        private void InitData()
        {
            sysUserInfo = SysUserManager.GetSysUserInfoById(int.Parse(Request.QueryString["SysUserId"]));
            if (sysUserInfo.SystemUserId > 0)
            {
                roleList = RolesManager.GetApplicationId(sysUserInfo.ApplicationId);
                userRoleList = RolesManager.GetListBySysUserId(sysUserInfo.SystemUserId);
                txt_RoleId.Value = userRoleList.Count > 0 ? userRoleList[0].RoleId.ToString() : "";
            }
        }

        private void Save()
        {
            var roleId = txt_RoleId.Value;
            if (string.IsNullOrEmpty(roleId) == false)
            {
                if (RolesManager.AddRolesToSysUser(new Guid(roleId), int.Parse(Request.QueryString["SysUserId"])))
                {
                    saveStatus = 1;
                }
                else
                {
                    saveStatus = 0;
                }
            }
        }
    }
}
